package com.zp.utils;

import com.zp.domain.vo.UserVo;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class CommonUtil {

    public static String dateToString(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static ArrayList<String> stringToArrayList(String stringList) {
        ArrayList<String> res = new ArrayList<>();
        return res;
    }

    public static ArrayList<Integer> stringToInteger(ArrayList<String> stringList) {
        ArrayList<Integer> integerList = new ArrayList<>();
        try {
            for(String str:stringList) {
                integerList.add(Integer.parseInt(str));
            }
        } catch (NumberFormatException e) {
            System.out.println("数字格式转换出错");
            e.printStackTrace();
        }
        return integerList;
    }

    public static Integer[] stringToInteger(String[] stringList) {
        Integer[] integerList = new Integer[stringList.length];
        try {
            for(int i = 0;i < stringList.length;i++) {
                integerList[i] = Integer.parseInt(stringList[i]);
            }
        } catch (NumberFormatException e) {
            System.out.println("数字格式转换出错");
            e.printStackTrace();
        }
        return integerList;
    }

    /**
     * 将读入的BufferedReader 转换成字符串
     * @param reader
     * @return
     */
    public static String readJSONData(BufferedReader reader) {
        String lineString = null;
        StringBuffer json = new StringBuffer();
        try {
            while ((lineString=reader.readLine())!=null) {
                json.append(lineString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

}
